package zx.x.modules.yshapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zx.x.common.utils.PageUtils;
import zx.x.common.utils.R;
import zx.x.modules.yshapp.dto.GoodsDto;
import zx.x.modules.yshapp.entity.GoodsEntity;
import zx.x.modules.yshapp.entity.PicAddressEntity;
import zx.x.modules.yshapp.service.GoodsService;
import zx.x.modules.yshapp.service.PicAddressService;

import java.util.*;


/**
 *
 * @author x
 * @email x
 */
@RestController
@RequestMapping("yshapp/goods")
public class GoodsAppController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private PicAddressService picAddressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = goodsService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 详情
     */
    @RequestMapping("/detail")
    public R detail(Integer id) {
        GoodsEntity goodsEntity = goodsService.getById(id);
        GoodsDto goodsDto = new GoodsDto();

        BeanUtils.copyProperties(goodsEntity, goodsDto);
        QueryWrapper<PicAddressEntity> picQw = new QueryWrapper<>();
        picQw.eq("goods_id", id)
                .eq("type", 1)
                .orderBy(true, true, "sort");

        goodsDto.setLbPicList(picAddressService.list(picQw));
        picQw = new QueryWrapper<>();
        picQw.eq("goods_id", id)
                .eq("type", 2)
                .orderBy(true, true, "sort");
        goodsDto.setDetailPicList(picAddressService.list(picQw));
        return R.ok().put("goodsDto", goodsDto);
    }

    /**
     * 首页福利商品
     */
    @RequestMapping("/homeGoodsList")
    public R homeGoodsList(Integer communityId){
        int count = goodsService.count(
                new QueryWrapper<GoodsEntity>().eq("on_sale",0)
                        .eq("delete_flag",0)
                        .eq("type",1)
                        .eq("community_id",communityId)
                        .orderBy(true,true,"id"));

        if (count==0) {
            return R.error("没有福利商品");
        }
        List<GoodsEntity> homeGoodsList = new ArrayList<>();
        if(count<3){//1，2个
            homeGoodsList = goodsService.list(new QueryWrapper<GoodsEntity>()
                    .eq("on_sale",0)
                    .eq("delete_flag",0)
                    .eq("type",1)
                    .eq("community_id",communityId));
        }else{
            Random rand = new Random();
            HashSet<Integer> set=new HashSet<Integer>();
            Integer randomNum ;
            while(set.size()<3){
                randomNum = rand.nextInt(count);
                set.add(randomNum);
            }
            for (Integer i : set) {
                homeGoodsList.addAll (goodsService.list(new QueryWrapper<GoodsEntity>()
                        .eq("on_sale",0)
                        .eq("delete_flag",0)
                        .eq("type",1)
                        .eq("community_id",communityId)
                        .last("limit "+i+",1")
                )) ;
            }
        }

        return R.ok().put("homeGoodsList", homeGoodsList);
    }
}
